//
//  InAppPurchaseManager.h
//  iStock Global
//
//  Created by Andy on 2011/1/25.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <StoreKit/StoreKit.h>
#import <UIKit/UIKit.h>
#import "ASIFormDataRequest.h"
#import "ASIHTTPRequest.h"
 

#define kInAppPurchaseManagerProductsFetchedNotification @"kInAppPurchaseManagerProductsFetchedNotification"
#define kInAppPurchaseManagerTransactionFailedNotification @"kInAppPurchaseManagerTransactionFailedNotification"
#define kInAppPurchaseManagerTransactionSucceededNotification @"kInAppPurchaseManagerTransactionSucceededNotification"
@protocol InAppPurchaseManagerDelegate

-(void)productsRequest_didReceiveResponse;
-(void)productsRequest_failedTransaction;
-(void)productsRequest_finishTransaction;
-(void)sucess_purchase;
-(void)productsRequest_check_iap_expire_date; 

-(void)start_ActivityIndicator_view:(NSString *)label_text progress_value:(float)prv; 
-(void)stop_ActivityIndicator_view; 

 @end

@interface InAppPurchaseManager : NSObject <SKProductsRequestDelegate,SKPaymentTransactionObserver>
{
    id <InAppPurchaseManagerDelegate> delgete;	
	
	SKProduct *proUpgradeProduct;
    SKProductsRequest *productsRequest;
	NSMutableData *decodedData;
	NSURLConnection *connection;
	
	//記載網路商品資料
	NSMutableArray *Product_title;
	NSMutableArray *Product_description;
	NSMutableArray *Product_price;
	NSMutableArray *Product_id;	
	
    //BOOL isRestoreRecipt_avoidLoop; //預防 因為訊息伺服器存有多筆的佇列訊息
    BOOL isRestoreHistoryRecipt;   
    
    NSString *r_bid;
    NSString *r_original_transaction_id;  
    NSString *r_item_id;
    NSString *r_transaction_id;
    NSString *r_quantity;
    NSString *r_bvrs;
    NSString *r_expires_date_formatted;
    NSString *r_original_purchase_date;
    NSString *r_expires_date;
    NSString *r_purchase_date;
    NSString *r_product_id;    
    
    NSString *lri_bid;
    NSString *lri_original_transaction_id;  
    NSString *lri_item_id;
    NSString *lri_transaction_id;
    NSString *lri_quantity;
    NSString *lri_bvrs;
    NSString *lri_expires_date_formatted;
    NSString *lri_original_purchase_date;
    NSString *lri_expires_date;
    NSString *lri_purchase_date;
    NSString *lri_product_id;        
    
    NSString *eri_bid;
    NSString *eri_original_transaction_id;  
    NSString *eri_item_id;
    NSString *eri_transaction_id;
    NSString *eri_quantity;
    NSString *eri_bvrs;
    NSString *eri_expires_date_formatted;
    NSString *eri_original_purchase_date;
    NSString *eri_expires_date;
    NSString *eri_purchase_date;
    NSString *eri_product_id;        
    
   
    //
    NSMutableArray *array_r_bid;
    NSMutableArray *array_r_original_transaction_id;  
    NSMutableArray *array_r_item_id;
    NSMutableArray *array_r_transaction_id;
    NSMutableArray *array_r_quantity;
    NSMutableArray *array_r_bvrs;
    NSMutableArray *array_r_expires_date_formatted;
    NSMutableArray *array_r_original_purchase_date;
    NSMutableArray *array_r_expires_date;
    NSMutableArray *array_r_purchase_date;
    NSMutableArray *array_r_product_id;    
    
    NSMutableArray *array_lri_bid;
    NSMutableArray *array_lri_original_transaction_id;  
    NSMutableArray *array_lri_item_id;
    NSMutableArray *array_lri_transaction_id;
    NSMutableArray *array_lri_quantity;
    NSMutableArray *array_lri_bvrs;
    NSMutableArray *array_lri_expires_date_formatted;
    NSMutableArray *array_lri_original_purchase_date;
    NSMutableArray *array_lri_expires_date;
    NSMutableArray *array_lri_purchase_date;
    NSMutableArray *array_lri_product_id;        
    
    NSMutableArray *array_eri_bid;
    NSMutableArray *array_eri_original_transaction_id;  
    NSMutableArray *array_eri_item_id;
    NSMutableArray *array_eri_transaction_id;
    NSMutableArray *array_eri_quantity;
    NSMutableArray *array_eri_bvrs;
    NSMutableArray *array_eri_expires_date_formatted;
    NSMutableArray *array_eri_original_purchase_date;
    NSMutableArray *array_eri_expires_date;
    NSMutableArray *array_eri_purchase_date;
    NSMutableArray *array_eri_product_id;      
    
    NSMutableArray *array_status;    
    
    NSMutableArray *temp_array_transaction_Restored;  
    int temp_array_transaction_Restored_index;
    
    NSMutableArray *temp_array__transaction_Restored_rawdata_json;
    
}
@property(assign) id <InAppPurchaseManagerDelegate> delgete;
@property (nonatomic, retain) NSMutableArray *Product_title;
@property (nonatomic, retain) NSMutableArray *Product_description;
@property (nonatomic, retain) NSMutableArray *Product_price;
@property (nonatomic, retain) NSMutableArray *Product_id;

 
- (void)requestProUpgradeProductData;
// public methods
- (void)loadStore;
- (BOOL)canMakePurchases;
- (void)purchaseProUpgrade:(NSString *)ID;
- (void)completeTransaction:(SKPaymentTransaction *)transaction;
- (void)restoreTransaction:(SKPaymentTransaction *)transaction;
- (void)failedTransaction:(SKPaymentTransaction *)transaction;
- (void)sucess_Non_Consumable:(NSString *)productId;
- (void)recordTransaction:(SKPaymentTransaction *)transaction;
- (void)finishTransaction:(SKPaymentTransaction *)transaction wasSuccessful:(BOOL)wasSuccessful;
- (void)restore_purchase;
- (NSString *)encode:(const uint8_t *)input length:(NSInteger)length;
- (NSString *)CpathForSymbol:(NSString *)aSymbol;
- (void)get_purchase_information:(SKPaymentTransaction *)transaction;
- (void)get_all_purchase_information:(SKPaymentTransaction *)transaction;

-(void)record_update_iap_expire_Finish:(ASIHTTPRequest *)request;
-(void)record_update_iap_expire_Fail:(ASIHTTPRequest *)request;

 
@end
